package cn.maihe.elg.operation.centers.follow.channel.dto;

import cn.maihe.elg.operation.centers.follow.enums.FollowRespCode;
import cn.maihe.elg.operation.model.dto.BaseResp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Description
 * @Author WJH
 * @Date 2021/04/19
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class FollowBaseRespDTO extends BaseResp {

    /**
     * 响应码
     */
    private String code;
    /**
     * 响应码描述
     */
    private String msg;

    @JsonIgnore
    private Object ReqObj;

    @JsonIgnore
    public boolean isSuccess() {
        return code != null && FollowRespCode.isSuccess(code);
    }


    public static FollowBaseRespDTO of(FollowRespCode status, String msg) {
        FollowBaseRespDTO respDTO = new FollowBaseRespDTO();
        respDTO.setCode(status.getRespCode());
        respDTO.setMsg(msg);
        return respDTO;
    }

    public static FollowBaseRespDTO success(String msg) {
        return FollowBaseRespDTO.of(FollowRespCode.SUCCESS, msg);
    }

    public static FollowBaseRespDTO error(String msg) {
        return FollowBaseRespDTO.of(FollowRespCode.FAILURE, msg);
    }

    public static <T extends FollowBaseRespDTO> T build(FollowRespCode statusEnum, String msg, Class<T> tClass) {
        try {
            T t = tClass.newInstance();
            t.setCode(statusEnum.getRespCode());
            t.setMsg(msg);
            return t;
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("初始化" + tClass.getName() + "异常：" + e.getMessage(), e);
        }
    }

    public static <T extends FollowBaseRespDTO> T success(String msg, Class<T> tClass) {
        return build(FollowRespCode.SUCCESS, msg, tClass);
    }

    public static <T extends FollowBaseRespDTO> T error(String msg, Class<T> tClass) {
        return build(FollowRespCode.FAILURE, msg, tClass);
    }

}
